package com.fowo.api.syncLingxingApi.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.lang.func.VoidFunc1;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fowo.api.service.handleWork.purchaseOrderPullPurchaseOrderAction;
import com.fowo.api.service.impl.LingXingInterfaceServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderProductServiceImpl;
import com.fowo.api.syncAmazon.api.ListingService;
import com.fowo.api.syncLingxingApi.service.syncImpl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;

@Service
@Slf4j
@DS("ds24")
public class SyncLxApiRunnerService {
    @Resource
    private SyncProductListServiceImpl productListService;

    @Resource
    private SyncLxBatchProductInfoServiceImpl batchProductInfoService;

    @Resource
    private SyncLxWarehouseServiceImpl syncLxWarehouseService;

    @Resource
    private SyncLxWarehousebinServiceImpl syncLxWarehousebinService;

    @Resource
    private SyncLxFbaListServiceImpl fbaListService;

    @Resource
    private SyncLxSupplierServiceImpl supplierService;

    @Resource
    private SyncLxSellerListServiceimpl sellerListServiceimpl;

    @Resource
    private LingXingInterfaceServiceImpl lingXingInterfaceService;

    @Resource
    private ListingService listingService;

    @Resource
    private purchaseOrderPullPurchaseOrderAction purchaseOrderProductService;

    @Resource
    TmpSyncLingxingToWarehousebinService tmpSyncLingxingToWarehousebinService;

    public void runWarehouseBin() {
        List<String> wids = tmpSyncLingxingToWarehousebinService.getWarehouseId();
        System.out.println(wids.toString());
        System.out.println(wids.size());

        for (int i = 0; i < wids.size(); ++i) {
            String wid = wids.get(i);
            log.info(">>>>>>>> 拉取 id = " + wid + ". " + i + "/" + wids.size() + ".");
            JSONObject requestBody = new JSONObject();
            requestBody.put("id", wid);

            syncLxWarehousebinService.mainLoop(requestBody);
        }
    }

    public void runOpening() {
        long start_time = System.currentTimeMillis();

        log.info("拉取产品列表");
        productListService.mainLoop(null);

        log.info("拉取仓库");
        syncLxWarehouseService.mainLoop(null);

        log.info("拉取Listing");
        lingXingInterfaceService.syncListing();

        log.info("拉取仓位");
        syncLxWarehousebinService.mainLoop(null);

        log.info("拉取供应商");
        supplierService.mainLoop(null);

        log.info("拉取店铺");
        sellerListServiceimpl.mainLoop(null);

        log.info("期初库存");
        try {
            lingXingInterfaceService.syncInventoryBinDetailsNews();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("拉取产品信息");
        batchProductInfoService.mainLoop(null);

        log.info("拉取销售出库单");
        lingXingInterfaceService.syncWmsOrderList();

        log.info("fba出库单");
        lingXingInterfaceService.syncStockOutToday();

        log.info("拉取销售订单");
        lingXingInterfaceService.syncSalesDailyListByDay(null);

        log.info("采购订单");
        try {
            purchaseOrderProductService.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        log.info("同步结束, 用时 " + String.valueOf(System.currentTimeMillis() - start_time) + " 毫秒.");
    }

    public void run() {
        long start_time = System.currentTimeMillis();

        log.info("拉取产品列表");
        productListService.mainLoop(null);

        log.info("拉取仓库");
        syncLxWarehouseService.mainLoop(null);

        log.info("拉取Listing");
        lingXingInterfaceService.syncListing();

        log.info("拉取仓位");
        syncLxWarehousebinService.mainLoop(null);

        log.info("拉取供应商");
        supplierService.mainLoop(null);

        log.info("拉取店铺");
        sellerListServiceimpl.mainLoop(null);

        log.info("拉取销售出库单");
        lingXingInterfaceService.syncWmsOrderList();

        log.info("拉取销售订单");
        lingXingInterfaceService.syncSalesDailyListByDay(null);

        log.info("采购订单");
        try {
            purchaseOrderProductService.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

//        log.info("拉取产品信息");
        batchProductInfoService.mainLoop(null);

        log.info("同步结束, 用时 " + String.valueOf(System.currentTimeMillis() - start_time) + " 毫秒.");

    }

    public void runV1() {
        List<SyncLxApiRunnerService.SnycEnum> list = CollUtil.newArrayList(SnycEnum.product);
        this.doInvoke(list);
    }

    public void runV2() {
        List<SyncLxApiRunnerService.SnycEnum> list = CollUtil.newArrayList(SyncLxApiRunnerService.SnycEnum.warehouse,SyncLxApiRunnerService.SnycEnum.warehousebin);
        this.doInvoke(list);
    }
    public void doInvoke(List<SnycEnum> snycEnumList) {
        long start_time = System.currentTimeMillis();
        for (SnycEnum snycEnum : snycEnumList) {
            log.info("拉取" + snycEnum.getMsg() + "开始");
            if (StrUtil.equals(SnycEnum.product.getValue(), snycEnum.getValue())) {
                productListService.mainLoop(null);
            } else if (StrUtil.equals(SnycEnum.listing.getValue(), snycEnum.getValue())) {
                lingXingInterfaceService.syncListing();
            } else if (StrUtil.equals(SnycEnum.warehouse.getValue(), snycEnum.getValue())) {
                syncLxWarehouseService.mainLoop(null);
            } else if (StrUtil.equals(SnycEnum.warehousebin.getValue(), snycEnum.getValue())) {
                syncLxWarehousebinService.mainLoop(null);
            } else if (StrUtil.equals(SnycEnum.supplier.getValue(), snycEnum.getValue())) {
                supplierService.mainLoop(null);
            } else if (StrUtil.equals(SnycEnum.shopinfo.getValue(), snycEnum.getValue())) {
                sellerListServiceimpl.mainLoop(null);
            } else if (StrUtil.equals(SnycEnum.wmsorder.getValue(), snycEnum.getValue())) {
                lingXingInterfaceService.syncWmsOrderList();
            } else if (StrUtil.equals(SnycEnum.salesdailylist.getValue(), snycEnum.getValue())) {
                lingXingInterfaceService.syncSalesDailyListByDay(null);
            } else if (StrUtil.equals(SnycEnum.purchaseorder.getValue(), snycEnum.getValue())) {
                try {
                    purchaseOrderProductService.run();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new RuntimeException("参数类型指定错误!");
            }
            log.info("拉取" + snycEnum.getMsg() + "结束");
        }
        log.info("同步结束, 用时 " + String.valueOf(System.currentTimeMillis() - start_time) + " 毫秒.");

    }

    public enum SnycEnum implements IEnum<String> {
        product("product", "产品"),

        listing("listing", "listing"),

        warehouse("warehouse", "仓库"),

        warehousebin("warehousebin", "仓位"),

        supplier("supplier", "供应商"),

        shopinfo("shopinfo", "店铺"),

        wmsorder("wmsorder", "销售出库单"),

        salesdailylist("salesdailylist", "销量统计"),

        purchaseorder("purchaseorder", "采购订单"),
        ;

        private final String value;
        private final String msg;

        SnycEnum(String value, String msg) {
            this.value = value;
            this.msg = msg;
        }

        @Override
        public String getValue() {
            return value;
        }

        public String getMsg() {
            return msg;
        }
    }
}
